package com.sqstudio.display.screen
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;

	/**
	 * 屏幕显示调整类
	 * 记录UI元素原始位置信息
	 * 收到ScreenDriver屏幕改变通知
	 * 
	 * 整个舞台分为9个区域可以添加显示元素进去，自动布局
	 * @author 翼翔天外
	 * 
	 */
	public class ScreenDisplay 
	{
		/**
		 * 下列表 
		 */
		private var _B_List:Vector.<DisplayVO> = new Vector.<DisplayVO>;		
		/**
		 * 右列表 
		 */
		private var _R_List:Vector.<DisplayVO> = new Vector.<DisplayVO>;		
		/**
		 * 水平居中
		 */
		private var _HC_List:Vector.<DisplayVO> = new Vector.<DisplayVO>;	
		/**
		 * 垂直居中
		 */
		private var _VC_List:Vector.<DisplayVO> = new Vector.<DisplayVO>;
		
		public function ScreenDisplay()
		{
			ScreenDriver.getInstance().addEventListener(ScreenDriver.SCREEN_RESIZE,onScreenResize,false,0,true);
		}
		
		private function onScreenResize(e:Event):void
		{
			var i:int,len:int;

			//更新下列表显示对象
			len = _B_List.length;
			for(i = 0 ; i < len ;i++)
			{
				_B_List[i].display.y = _B_List[i].position.y + ScreenDriver.getInstance().nowHeight - ScreenDriver.getInstance().baseHeight;
			}
			
			//更新右列表显示对象
			len = _R_List.length;
			for(i = 0 ; i < len ;i++)
			{
				_R_List[i].display.x = _R_List[i].position.x + ScreenDriver.getInstance().nowWidth - ScreenDriver.getInstance().baseWidth;
			}
			
			//更新水平居中列表显示对象
			len = _HC_List.length;
			for(i = 0 ; i < len ;i++)
			{
				_HC_List[i].display.x = _HC_List[i].position.x + (ScreenDriver.getInstance().nowWidth - ScreenDriver.getInstance().baseWidth)/2;
			}
			
			//更新垂直居中列表显示对象
			len = _VC_List.length;
			for(i = 0 ; i < len ;i++)
			{
				_VC_List[i].display.y = _VC_List[i].position.y + (ScreenDriver.getInstance().nowHeight - ScreenDriver.getInstance().baseHeight)/2;
			}
		}
		
		
		private function buildDisplayVO(mc:DisplayObject):DisplayVO
		{
			if(ScreenDriver.getInstance().stage ==null){
				throw new Error("ScreenDriver 没有初始化！ 先执行：ScreenDriver.getInstance().init(...)");
			}
			
			
			var vo:DisplayVO = new DisplayVO();
			vo.display = mc;
			vo.position =  new Point(mc.x,mc.y);
			return vo;
		}
		/**
		 *刷新显示（手动） 
		 * 
		 */		
		public function updateSize():void{
			onScreenResize(null);
		}
		
		/**
		 * 左上 
		 * @param mc
		 *   
		 */
		public function addTL(mc:DisplayObject,...args):void{
			//左上位置不用移动
		}
		
		/**
		 * 右上 
		 * @param mc
		 * 
		 */
		public function addTR(mc:DisplayObject,...args):void{
			_R_List.push(buildDisplayVO(mc));
			for (var i:uint = 0; i < args.length; i++) 
			{
				_R_List.push(buildDisplayVO(args[i]));
			}
		}
		
		/**
		 * 中上 
		 * @param mc
		 * 
		 */
		public function addT(mc:DisplayObject,...args):void{
			_HC_List.push(buildDisplayVO(mc));
			for (var i:uint = 0; i < args.length; i++) 
			{
				_HC_List.push(buildDisplayVO(args[i]));
			}
		}
		
		/**
		 * 左中 
		 * @param mc
		 * 
		 */
		public function addL(mc:DisplayObject,...args):void{
			_VC_List.push(buildDisplayVO(mc));
			for (var i:uint = 0; i < args.length; i++) 
			{
				_VC_List.push(buildDisplayVO(args[i]));
			}
		}
		
		
		/**
		 * 中中 
		 * @param mc
		 * 
		 */
		public function addC(mc:DisplayObject,...args):void{
			_HC_List.push(buildDisplayVO(mc));
			_VC_List.push(buildDisplayVO(mc));
			for (var i:uint = 0; i < args.length; i++) 
			{
				_HC_List.push(buildDisplayVO(args[i]));
				_VC_List.push(buildDisplayVO(args[i]));
			}
		}
		
		/**
		 * 右中 
		 * @param mc
		 * 
		 */
		public function addR(mc:DisplayObject,...args):void{
			_R_List.push(buildDisplayVO(mc));
			_VC_List.push(buildDisplayVO(mc));
			for (var i:uint = 0; i < args.length; i++) 
			{
				_R_List.push(buildDisplayVO(args[i]));
				_VC_List.push(buildDisplayVO(args[i]));
			}
		}
		
		/**
		 * 下中 
		 * @param mc
		 * 
		 */
		public function addB(mc:DisplayObject,...args):void{
			_B_List.push(buildDisplayVO(mc));
			_HC_List.push(buildDisplayVO(mc));
			for (var i:uint = 0; i < args.length; i++) 
			{
				_B_List.push(buildDisplayVO(args[i]));
				_HC_List.push(buildDisplayVO(args[i]));
			}
		}
		
		
		/**
		 * 下左 
		 * @param mc
		 * 
		 */
		public function addBL(mc:DisplayObject,...args):void{
			_B_List.push(buildDisplayVO(mc));
			for (var i:uint = 0; i < args.length; i++) 
			{
				_B_List.push(buildDisplayVO(args[i]));
			}
		}
		
		/**
		 *下右 
		 * @param mc
		 * 
		 */
		public function addBR(mc:DisplayObject,...args):void{
			_B_List.push(buildDisplayVO(mc));
			_R_List.push(buildDisplayVO(mc));
			for (var i:uint = 0; i < args.length; i++) 
			{
				_B_List.push(buildDisplayVO(args[i]));
				_R_List.push(buildDisplayVO(args[i]));
			}
		}
		
		public function destroy():void
		{			
			ScreenDriver.getInstance().removeEventListener(ScreenDriver.SCREEN_RESIZE,onScreenResize);
			_B_List = null;		
			_R_List = null;		
			_HC_List = null;	
			_VC_List = null;
		}
	}
}
import flash.display.DisplayObject;
import flash.geom.Point;

class DisplayVO
{
	/**
	 * 显示对象 
	 */
	public var display:DisplayObject;
	/**
	 * 初始位置 
	 */
	public var position:Point;
}